/**
 * @FileName: Utils.java
 * @Author
 * @Description:
 * @Date 2016年5月3日 下午2:22:42
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.util;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;

import cn.hpclub.server.constant.Constant;

public class Utils{
    private static final Logger logger     = LoggerFactory.getLogger(Utils.class);
    private static final int    MAX_LENGTH = 128;

    /**
     * json数据封装成map
     * 
     * @return
     */
    public static ConcurrentHashMap<String, String> getMapDataFromJson(String jsonData){
        ConcurrentHashMap<String, String> mapData = new ConcurrentHashMap<String, String>();
        if(!StrKit.notBlank(jsonData) || !jsonData.contains("{")){
            return mapData;
        }
        JSONObject jo = JSONObject.parseObject(jsonData);
        for(Entry<String, Object> item : jo.entrySet()){
            String value = String.valueOf(item.getValue());
            if(StrKit.notBlank(value)){
                mapData.put(item.getKey(), value);
            }
        }
        return mapData;
    }

    public static boolean isMobileNO(String mobiles){
        Pattern p = Pattern.compile(Constant.Regex.MOBILE);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static String getShortMsg(String msg){
        if(msg == null){
            return "";
        }

        if(msg.length() <= MAX_LENGTH){
            return msg;
        } else{
            return msg.substring(0, MAX_LENGTH);
        }

    }

    public static String getShortMsg(String msg, int length){
        if(msg == null){
            return "";
        }

        if(msg.length() <= length){
            return msg;
        } else{
            return msg.substring(0, length);
        }

    }

    /**
     * 将含有分隔符的字符串转换为数字数组 例如 1#2#3#4
     * @param ids
     * @param token
     * @return  Integer[]
     */
    public static Integer[] string2Int(String ids, String token){
        if(StrKit.isBlank(ids)){
            return new Integer[0];
        }
        String[] strings = ids.split(token);
        Integer[] result = new Integer[strings.length];
        for(int i = 0; i < result.length; i++){
            result[i] = Integer.parseInt(strings[i]);
        }
        return result;
    }

    /**
     * 数组转换为字符串，例如[1,2,3] , -->1,2,3
     * @param arr
     * @return
     */
    public static  String join(Object[] arr,String token){
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++){
            if(i == arr.length - 1){
                sb.append(arr[i].toString());
            } else{
                sb.append(arr[i].toString()).append(token);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(Utils.join(new String[]{"1","2","3"},","));
        System.out.println(Utils.join(new Integer[]{1},","));
    }
}
